/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ocbn.remotelab.events;

import javax.microedition.media.MediaException;
import net.rim.device.api.system.Application;
import ocbn.remotelab.GUI.NotifyingGUI;
import net.rim.device.api.ui.*;
import ocbn.remotelab.client.EventListener;
import net.rim.device.api.ui.component.*;
import ocbn.remotelab.xml.*;
import java.util.Vector;
import ocbn.remotelab.GUI.playMusic;


/**
 * Handles any event(not just mass spectrometer events) by calling the
 * appropriate methods according to the event. Handling mass spectrometer events
 * for now e.g. if its an emergency, sorting events into a list. etc.
 * @author Basem
 */
public class EventHandler implements EventListener {
    private playMusic p = new playMusic();
    /**
     * Object representing the incoming event.
     */
    private Event incoming;

    /**
     * Constructor for the EventHandler.
     * @param incoming The incoming event.
     */
    /**
     * Will be called when an Event arrives.
     * @param e Event.
     */
    public void EventReceived(Event e) {
        incoming = e;
        //If its a MassSpec event and its an emegency call the fail dialog.
        if (incoming.getType() == MassSpectrometerStateEvent.class) {
            if (incoming.isImportant()) {
                synchronized (Application.getEventLock()) {
                    String[] s = {"Respond", "Ignore"};
                    NotifyingGUI screen = new NotifyingGUI
                            (incoming.toString(),s);

                    screen.setDialogClosedListener(new DialogClosedListener() {
                        public void dialogClosed
                                (Dialog aTheDialog, int aDialogReturnValue) {
                            if (aDialogReturnValue == Dialog.YES) {                                
                                /**
                                 * For future implementations
                                 */
          
                            } else {
                                /**
                                 * For future implementations
                                 */
                            }
                        }

        });
                    UiEngine ui = Ui.getUiEngine();
                    ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_QUEUE);
                }

            }

            //Create a wrapper vector to pass in XML export.
            Vector wrapper = new Vector();
            wrapper.addElement(incoming);
            String eventString = XMLExport.exportXML(wrapper);

            //calling FileStorage.
            FileStorage.appendXML("file:///SDCard/BlackBerry/MS.txt", eventString);
    
        }

       

    }

    //For testing.
    public void packetReceived(String packet) {
        throw new UnsupportedOperationException("Not supported yet.");

    }
}


    
  

